package com.googlecode.alfrescocachingserver.model;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

public class WebScriptCacheKey implements Serializable
{
    private static final long serialVersionUID = 7290151419171947339L;

    private static final String DELIMITER = "¦¦";
    
    protected String contentType;
    protected WebScriptUri webScriptUri;

    public WebScriptCacheKey()
    {
    }

    public WebScriptCacheKey(String delimitedCacheKey)
    {
        String[] values = delimitedCacheKey.split(DELIMITER);
        
        this.webScriptUri = new WebScriptUri(values[0], values[1]);

        if (values.length > 2)
        {
            this.contentType = values[2];
        }
    }

    public WebScriptCacheKey(WebScriptUri webScriptUri, String contentType)
    {
        this.contentType = contentType;
        this.webScriptUri = webScriptUri;
    }

    public String getContentType()
    {
        return contentType;
    }

    public void setContentType(String contentType)
    {
        this.contentType = contentType;
    }

    public WebScriptUri getWebScriptUri()
    {
        return webScriptUri;
    }

    public void setWebScriptUri(WebScriptUri webScriptUri)
    {
        this.webScriptUri = webScriptUri;
    }
    
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((contentType == null) ? 0 : contentType.hashCode());
        result = prime * result + ((webScriptUri == null) ? 0 : webScriptUri.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        
        if (obj == null)
            return false;
        
        if (getClass() != obj.getClass())
            return false;
        
        WebScriptCacheKey other = (WebScriptCacheKey) obj;
        if (contentType == null)
        {
            if (other.contentType != null)
                return false;
        }
        else if (!contentType.equals(other.contentType))
            return false;
        
        if (webScriptUri == null)
        {
            if (other.webScriptUri != null)
                return false;
        }
        else if (!webScriptUri.equals(other.webScriptUri))
            return false;
        
        return true;
    }

    @Override
    public String toString()
    {
        StringBuilder buf = new StringBuilder();
        
        buf.append(webScriptUri.getHttpMethod())
           .append(DELIMITER)
           .append("/")
           .append(webScriptUri.getEndpoint())
           .append("/s")
           .append(webScriptUri.getUriWithQueryString())
           .append(DELIMITER);
        
        if (StringUtils.isNotBlank(contentType))
        {
            buf.append(contentType);
        }
        
        return buf.toString();
    }
}
